package com.xdhl.fu.fim.price.strategy;

import com.xdhl.fu.common.core.utils.MathUtils;
import com.xdhl.fu.flight.info.api.domain.FlightInfo;
import com.xdhl.fu.flight.info.api.domain.FlightSchedule;
import org.springframework.stereotype.Component;

/**
 * 时间段价格策略
 *
 * @author 朱力维
 * @time 2022/7/3 11:37
 */
@Component
public class TimeSlotPriceStrategy implements PriceCalcStrategy {

    /**
     * 早班折扣
     */
    public static final double LATER_DISCOUNT = 0.7f;

    /**
     * 晚班折扣
     */
    public static final double EARLY_DISCOUNT = 0.4f;

    /**
     * 折扣模式【1. 早班 2. 晚班】
     */
    private int mode = 0;

    @Override
    public int order() {
        return 4;
    }

    @Override
    public PriceCalcStrategy calcEconomy(FlightInfo flightInfo, FlightSchedule schedule) {
        Double price;
        if (mode == 1) {
            price = MathUtils.priceMultip(EARLY_DISCOUNT, schedule.getEconomyPrice());
        } else {
            price = MathUtils.priceMultip(LATER_DISCOUNT, schedule.getEconomyPrice());
        }
        schedule.setEconomyPrice(price);
        return this;
    }

    @Override
    public PriceCalcStrategy calcBusiness(FlightInfo flightInfo, FlightSchedule schedule) {
        Double price;
        if (mode == 1) {
            price = MathUtils.priceMultip(EARLY_DISCOUNT, schedule.getBusinessPrice());
        } else {
            price = MathUtils.priceMultip(LATER_DISCOUNT, schedule.getBusinessPrice());
        }
        schedule.setBusinessPrice(price);
        return this;
    }

    @Override
    public PriceCalcStrategy calcHeader(FlightInfo flightInfo, FlightSchedule schedule) {
        Double price;
        if (mode == 1) {
            price = MathUtils.priceMultip(EARLY_DISCOUNT, schedule.getHeadPrice());
        } else {
            price = MathUtils.priceMultip(LATER_DISCOUNT, schedule.getHeadPrice());
        }
        schedule.setHeadPrice(price);
        return this;
    }

    @Override
    public boolean enable(FlightInfo flightInfo, FlightSchedule schedule) {
        int hour = schedule.getStartTime().getHour();
        // 早班
        if (hour < 10) {
            mode = 1;
        } else if (hour > 16) { // 晚班
            mode = 2;
        }
        return mode != 0;
    }

}
